package com.hjy.cabinet.db;

import android.util.Log;

import com.hjy.cabinet.db.domain.CabinetInfo;

import java.util.ArrayList;
import java.util.List;

public class CabinetInfoInstance {
    private static CabinetInfoInstance instance;

    private CabinetInfoHelper dbHelper;

    private CabinetInfoInstance() {
    }

    public static CabinetInfoInstance getInstance() {
        if (instance == null) {
            synchronized (CabinetInfoInstance.class) {
                if (instance == null) {
                    instance = new CabinetInfoInstance();
                }
            }
        }
        return instance;
    }

    /**
     * 添加配置项
     *
     * @param cabinetInfo
     * @return 返回插入的行数
     */
    public long addConfig(CabinetInfo cabinetInfo) {
        try {
            return dbHelper.addCabinet(cabinetInfo);
        } catch (Exception e) {
            Log.e("CabinetInfoInstance", "Error adding cabinet: " + e.getMessage(), e);
            return -1; // 返回-1表示失败
        }
    }

    /**
     * 更新配置项
     *
     * @param cabinetInfo
     * @return 返回更新的行数
     */
    public int updateCabinet(CabinetInfo cabinetInfo) {
        try {
            return dbHelper.updateCabinet(cabinetInfo);
        } catch (Exception e) {
            Log.e("CabinetInfoInstance", "Error updating cabinet: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被更新
        }
    }

    /**
     * 删除配置项
     *
     * @param id 配置项ID
     * @return 返回删除的行数
     */
    public int deleteCabinet(int id) {
        try {
            return dbHelper.deleteCabinet(id);
        } catch (Exception e) {
            Log.e("CabinetInfoInstance", "Error deleting cabinet: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被删除
        }
    }

    /**
     * 获取配置项
     *
     * @param id 配置项ID
     * @return 返回配置项
     */
    public CabinetInfo getCabinet(int id) {
        try {
            return dbHelper.getCabinet(id);
        } catch (Exception e) {
            Log.e("CabinetInfoInstance", "Error retrieving cabinet by ID: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    /**
     * 获取所有配置
     *
     * @return List<CabinetInfo>
     */
    public List<CabinetInfo> getAllCabinets() {
        try {
            return dbHelper.getAllCabinets();
        } catch (Exception e) {
            Log.e("CabinetInfoInstance", "Error retrieving all cabinets: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    public void setInstance(CabinetInfoHelper cabinetInfoHelper) {
        this.dbHelper = cabinetInfoHelper;
    }
}